﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Players;
using Games;

namespace Rooms
{
    /// <summary>
    /// class contains infomation of a room
    /// </summary>
    public class Room
    {
        /// <summary>
        /// Id room
        /// </summary>
        int iIdRoom;

        public int IIdRoom
        {
            get { return iIdRoom; }
            set { iIdRoom = value; }
        }
        /// <summary>
        /// room number
        /// </summary>
        int iNumberRoom;

        public int INumberRoom
        {
            get { return iNumberRoom; }
            set { iNumberRoom = value; }
        }
        /// <summary>
        /// room name
        /// </summary>
        string strRoomName;

        public string StrRoomName
        {
            get { return strRoomName; }
            set { strRoomName = value; }
        }
        /// <summary>
        /// status of room
        /// </summary>
        int status;

        public int Status
        {
            get { return status; }
            set { status = value; }
        }
        /// <summary>
        ///  max player of room 
        /// </summary>
        int iMaxPlay;

        public int IMaxPlay
        {
            get { return iMaxPlay; }
            set { iMaxPlay = value; }
        }
        /// <summary>
        /// number player current of room
        /// </summary>
        int iCountPlayer;

        public int ICountPlayer
        {
            get { return iCountPlayer; }
            set { iCountPlayer = value; }
        }
        /// <summary>
        /// the maximum bet money
        /// </summary>
        double dMaxBet;

        public double DMaxBet
        {
            get { return dMaxBet; }
            set { dMaxBet = value; }
        }
        /// <summary>
        /// list player of room
        /// </summary>
        List<Player> lstPlayers;

        public List<Player> LstPlayers
        {
            get { return lstPlayers; }
            set { lstPlayers = value; }
        }
        /// <summary>
        /// list room
        /// </summary>
        static List<Room> lstRooms;

        public List<Room> LstRooms
        {
            get { return Room.lstRooms; }
            set { Room.lstRooms = value; }
        }

        Game objGame;

        public Game ObjGame
        {
            get { return objGame; }
            set { objGame = value; }
        }
        public Room() { }

        /// <summary>
        /// constructor when player first create room
        /// </summary>
        /// <param name="IdRoom"></param>
        /// <param name="NumberRoom"></param>
        /// <param name="RoomName"></param>
        /// <param name="MaxPlay"></param>
        /// <param name="MaxBet"></param>
        /// <param name="PlayFirst"></param>
        public Room(Game game,int IdRoom, int NumberRoom, string RoomName, int MaxPlay, double MaxBet, Player PlayFirst)
        {
            ObjGame = game;
            iIdRoom = IdRoom;
            iNumberRoom = NumberRoom;
            strRoomName = RoomName;
            iMaxPlay = MaxPlay;
            dMaxBet = MaxBet;
            iCountPlayer++;
        }
        /// <summary>
        /// player join room
        /// </summary>
        /// <param name="IdRoom"></param>
        /// <param name="Play"></param>
        /// <returns></returns>
        public static bool joinRoom(Room _Room, Player Play)
        {
            //Room selectRoom = this.LstRooms.Find(x => x.iIdRoom == IdRoom);
            if (_Room.LstPlayers == null)
                _Room.LstPlayers = new List<Player>();
            if (_Room != null && _Room.iCountPlayer < _Room.iMaxPlay)
            {
                _Room.LstPlayers.Add(Play);
                _Room.iCountPlayer++;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// player out room
        /// </summary>
        /// <param name="IdRoom"></param>
        /// <param name="Play"></param>
        /// <returns></returns>
        public static bool outRoom(Room _Room, Player Play)
        {
            //Room selectRoom = this.LstRooms.Find(x => x.iIdRoom == IdRoom);
            if (_Room != null && _Room.iCountPlayer > 0)
            {
                if (_Room.LstPlayers == null)
                    _Room.LstPlayers = new List<Player>();
                if (_Room.LstPlayers.Remove(Play))
                {
                    _Room.iCountPlayer--;
                    return true;
                }
                else
                    return false;
            }
            else
            {
                return false;
            }
        }
    }
}
